export default class Utils {

    public static monthArr: string[] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"];



    /**
     * 获取当前年月日
     * @returns 
     */
    public static getYearMonthDay(): string {
        var date = new Date();
        let year = date.getFullYear(); //获取完整的年份(4位)
        let month = date.getMonth(); //获取当前月份(0-11,0代表1月)
        let day = date.getDate(); //获取当前日(1-31)
        //March 2,2021
        return `${Utils.monthArr[month]} ${day},${year}`;
    }


    /**
     * 设置子节点排序-从上到下
     * @param con 父节点
     */
    public static setConChildIndex(con: cc.Node) {
        let arr = con.children;
        arr.sort((a, b) => {
            if (a.y < b.y) {
                return 1;
            } else if (a.y > b.y) {
                return -1;
            } else {
                return 0;
            }
        })
        arr.forEach((node: cc.Node, index: number) => {
            node.setSiblingIndex(index);
        }, this)
    }

    /**
     * 设置子节点排序-从下到上
     * @param con 父节点
     */
    public static setConChildIndex2(con: cc.Node) {
        let arr = con.children;
        arr.sort((a, b) => {
            if (a.y < b.y) {
                return -1;
            } else if (a.y > b.y) {
                return 1;
            } else {
                return 0;
            }
        })
        arr.forEach((node: cc.Node, index: number) => {
            node.setSiblingIndex(index);
        }, this)
    }

    /**
     * 将一个点转换到节点 (局部) 空间坐标系。
     * @param target 
     * @param otherNode 
     * @returns 
     */
    public static getLocalPositionWithOtherNode(target: cc.Node, otherNode: cc.Node): cc.Vec3 {
        if (!otherNode.parent) return null;
        let vec3 = otherNode.parent.convertToWorldSpaceAR(otherNode.position);//先转换成世界坐标
        vec3 = target.convertToNodeSpaceAR(vec3);
        return vec3;
    }

    /**
    * 将一个点转换到节点 (局部) 空间坐标系。
    */
    public static getWorldPositionWithOtherNode(target: cc.Node, otherNode, point?: cc.Vec3): cc.Vec3 {
        if (!otherNode.parent) return null;
        let vec3 = otherNode.parent.convertToWorldSpaceAR(point ? point : otherNode.position);//先转换成世界坐标
        vec3 = target.convertToNodeSpaceAR(vec3);
        return vec3;
    }


    /*
    * 格式化时间数为两位数
    * @param  {number} t 时间数
    * @returns String
    */
    public static formatTimeNum(t: number, b = true): string {
        if (b) {
            return t >= 10 ? t.toString() : "0" + t;
        } else {
            return t.toString();
        }
    }

    /**
    * 
    * @param str 加载精灵图
    * @param sprite 
    */
    public static loadSprite(str: string, sprite: cc.Sprite) {
        //加载标题
        cc.loader.loadRes(str, cc.SpriteFrame, function (err, spriteFrame) {
            if (err) return;
            sprite.spriteFrame = spriteFrame;
        });
    }

    /**
       * 设置灰色滤镜
       * @param node node节点 或 label、sprite组件
       * @param gray true变灰
       * @param children 子项是否变灰 默认false 只有node会有子项
       */
    public static setGray(node: cc.Node | cc.Label | cc.Sprite, gray: boolean = true, children?: boolean): void {
        let target: any = node;
        let str = gray ? cc.Material.BUILTIN_NAME.GRAY_SPRITE : cc.Material.BUILTIN_NAME.SPRITE;
        var material = cc.Material.createWithBuiltin(str + "");
        // material.define("USE_TEXTURE", true, 0);
        if (node instanceof cc.Node) {
            target = node.getComponent(cc.Sprite);
            target && target.setMaterial(0, material);
            target = node.getComponent(cc.Label);
            target && target.setMaterial(0, material);
            children && node.children.forEach(subNode => {
                Utils.setGray(subNode, gray, children)
            }, this)
        } else {
            target && target.setMaterial(0, material);
        }
    }
    public static getBrowserValue(value) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == value) {
                return pair[1];
            }
        }
        return null;
    }
}